package game;

import javax.microedition.khronos.opengles.GL10;

import gfx.Camera;
import gfx.Glyphs;
import gfx.Image;
import gfx.PressableButton;
import gfx.Spritesheet;
import util.GameClock;
import util.GameVars;
import util.InteractableInterface;
import util.Logger;
import util.VertexBuffer;

import android.graphics.PointF;

public class Menu {
	private PressableButton currentButton;
	private int currentButtonNum;

	private short buttonArrayIterator;
	private short touchIterator;
	private short nextGameLayer = GameVars.GAME_LAYER_MENU;
	
	private short currentLayer = GameVars.MENU_LAYER_MENU;
	private short nextLayer = -1;
	
	private boolean isLoaded = false;
	private boolean inMenu = true;
	private boolean close = false;
	
	private PressableButton[] buttonArray;
	
	public Menu(){
		isLoaded = false;
	}
	
	public void loadMenu(short character, short world, short stage){
		load();
		
		currentLayer = GameVars.MENU_LAYER_STAGE_SELECT;
	}
	
	public void load(){		

		buttonArray = new PressableButton[5];
		buttonArray[0] = new PressableButton(GameVars.BUTTON_TYPE_MENU, "Play", null, 3, .75f, GameVars.BUTTON_NUM_PLAY);
		buttonArray[1] = new PressableButton(GameVars.BUTTON_TYPE_MENU, "High Score", null, 3, .75f, GameVars.BUTTON_NUM_HIGH_SCORES);
		buttonArray[2] = new PressableButton(GameVars.BUTTON_TYPE_MENU, "Options", null, 3, .75f, GameVars.BUTTON_NUM_OPTION);
		buttonArray[3] = new PressableButton(GameVars.BUTTON_TYPE_MENU, "Extras", null, 3, .75f, GameVars.BUTTON_NUM_EXTRAS);
		buttonArray[4] = new PressableButton(GameVars.BUTTON_TYPE_MENU, "Quit", null, 3, .75f, GameVars.BUTTON_NUM_QUIT);
		
		for(buttonArrayIterator = 0; buttonArrayIterator < buttonArray.length; ++buttonArrayIterator)
			buttonArray[buttonArrayIterator].setCenter(2.5f + buttonArrayIterator%2, 5.5f - buttonArrayIterator - buttonArrayIterator * .25f);
		
		isLoaded = true;
	}
	
	public void draw(GL10 gl){
		switch(currentLayer){
		case GameVars.MENU_LAYER_MENU:
				for(buttonArrayIterator = 0; buttonArrayIterator < buttonArray.length; ++buttonArrayIterator){
					buttonArray[buttonArrayIterator].draw(gl);
				}
			break;
		}
	}
	
	// TODO Depending on the memory load, investigate changing these to boolean of show instead, and keeping them loaded until stage launch.
	public void update(long time){
		if(nextLayer != -1){
			currentLayer = nextLayer;
			nextLayer = -1;
		}
		switch(currentLayer){
		case GameVars.MENU_LAYER_STAGE_SELECT:
			break;
		case GameVars.MENU_LAYER_MENU:
			break;
		}
	}

	public boolean isLoaded(){ return isLoaded; }
	public boolean inMenu(){ return inMenu; }
	public void toggleMenu(){ inMenu = !inMenu; }
	public short getNextGameLayer(){ return nextGameLayer; }
	public boolean close(){ return close; }
	
	public void buttonHandler(){
		switch(currentButtonNum){
		case GameVars.BUTTON_NUM_PLAY:
//			if(currentLayer == GameVars.MENU_LAYER_MENU){
//				nextLayer = GameVars.MENU_LAYER_STAGE_SELECT;
//			}
//			else{ nextGameLayer = GameVars.GAME_LAYER_STAGE; inMenu = false;}
			nextGameLayer = GameVars.GAME_LAYER_STAGE; inMenu = false;
			break;
		case GameVars.BUTTON_NUM_BACK:
			nextLayer = (short) (currentLayer - 1);
			break;
		case GameVars.BUTTON_NUM_NEXT:
			Logger.log("Next Pressed");
			nextLayer = (short) (currentLayer + 1);
			break;
		case GameVars.BUTTON_NUM_HIGH_SCORES:
			break;
		case GameVars.BUTTON_NUM_OPTION:
			break;
		case GameVars.BUTTON_NUM_EXTRAS:
			break;
		case GameVars.BUTTON_NUM_QUIT:
			close = true;
			break;
		}
	}
	
	public void handleTouch(PointF loc){
		switch(currentLayer){
		case GameVars.MENU_LAYER_MENU:
			for(touchIterator = 0; touchIterator < buttonArray.length; ++touchIterator)
				if(buttonArray[touchIterator].isPressed(loc))
					currentButton = buttonArray[touchIterator];
			break;
		case GameVars.MENU_LAYER_STAGE_SELECT:
			break;
		}
	}

	public void handleDrag(PointF location) {
		
	}

	public boolean handleUp(PointF location) {
		if(currentButton != null)
			if(currentButton.isPressed(location)){
				currentButtonNum = currentButton.getNum();
				currentButton = null;
				buttonHandler();
				return true;
			}
		currentButton = null;
		if(currentLayer == GameVars.MENU_LAYER_STAGE_SELECT){
		}
		return false;
	}
	
	public boolean handleBackPress(){
		if(currentLayer == GameVars.MENU_LAYER_MENU){
			return true;
		}
		nextLayer = (short) (currentLayer - 1);
		return false;
	}
}
